package cn.stronglink.oil.map;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.ResponseHandlerInterface;
import com.loopj.android.http.TextHttpResponseHandler;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.List;

import cn.stronglink.oil.R;
import cn.stronglink.oil.entity.CommonResult;
import cn.stronglink.oil.entity.DeviceRealTimeInfo;
import cn.stronglink.oil.entity.GasHistoryInfo;
import cn.stronglink.oil.entity.GasType;
import cn.stronglink.oil.exceptions.BusinessErrorException;
import cn.stronglink.oil.mq.DeviceGasDataManager;
import cn.stronglink.oil.user.UserInfo;
import cn.stronglink.oil.utils.HttpClientUtils;
import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.HttpResponse;

/**
 * Created by chen on 2017/10/19.
 */

public class DeviceInfoPresenter {
    private Activity activity;
    private DeviceInfoView view;
    private static final int HISTORY_SUCCESS = 1, HISTORY_FAIL = 2, GAS_TYPE_SUCCESS = 3, GAS_TYPE_FAIL = 4;
    private String errMsg;
    private GasHistoryInfo gasHistoryInfos;
    private List<GasType> gasTypeList;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            view.hideProgress();
            switch (msg.what) {
//                case HISTORY_SUCCESS:
//                    view.setHistory(gasHistoryInfos);
//                    break;
//                case HISTORY_FAIL:
//                    if (errMsg == null) {
//                        view.showException(new BusinessErrorException(activity.getString(R.string.history_get_fail)));
//                    } else {
//                        view.showException(new BusinessErrorException(errMsg));
//                    }
//                    break;
                case GAS_TYPE_SUCCESS:
                    view.setGasType(gasTypeList);
                    break;
                case GAS_TYPE_FAIL:
                    if (errMsg == null) {
                        view.showException(new BusinessErrorException(activity.getString(R.string.history_get_fail)));
                    } else {
                        view.showException(new BusinessErrorException(errMsg));
                    }
                    break;
            }
        }
    };

    public DeviceInfoPresenter(Activity activity, DeviceInfoView view) {
        this.activity = activity;
        this.view = view;
    }

    public void getHistoryInfo(String controlDeviceSerial, int pageOffset, int pageSize) {
        view.showProgress();
        try {
            List<DeviceRealTimeInfo> deviceRealTimeInfos = DeviceGasDataManager.getInstance().getDeviceGasInfos();
            view.setHistory(deviceRealTimeInfos);
            view.hideProgress();
        } catch (Exception e) {
            view.hideProgress();
            view.showException(new BusinessErrorException(activity.getString(R.string.history_get_fail)));
        }
    }

    public void getGasType() {
        view.showProgress();
        HttpClientUtils.getGasType(new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                errMsg = null;
                handler.sendEmptyMessage(GAS_TYPE_FAIL);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                if (statusCode == HttpURLConnection.HTTP_OK && responseString != null && !responseString.isEmpty()) {
                    Gson gson = new Gson();
                    CommonResult<List<GasType>> commonResult = gson.fromJson(responseString,
                            new TypeToken<CommonResult<List<GasType>>>() {
                            }.getType());
                    if (commonResult.getErrorMessage() == null && commonResult.isSuccess()) {
                        gasTypeList = commonResult.getData();
                        handler.sendEmptyMessage(GAS_TYPE_SUCCESS);
                    } else {
                        errMsg = null;
                        errMsg = commonResult.getErrorMessage();
                        handler.sendEmptyMessage(GAS_TYPE_FAIL);
                    }
                } else {
                    errMsg = null;
                    handler.sendEmptyMessage(GAS_TYPE_FAIL);
                }
            }
        });
    }

    public void getSelectedHistory(int gasType, int deviceId) {
        view.showProgress();
        try {
            List<DeviceRealTimeInfo> deviceRealTimeInfos = null;
            if (gasType == -1 && deviceId == -1) {
                deviceRealTimeInfos = DeviceGasDataManager.getInstance().queryDeviceGasInfos(null, null);
            }
            if (gasType == -1 && deviceId != -1) {
                deviceRealTimeInfos = DeviceGasDataManager.getInstance().queryDeviceGasInfos(null, deviceId);
            }
            if(gasType!=-1 && deviceId==-1){
                deviceRealTimeInfos = DeviceGasDataManager.getInstance().queryDeviceGasInfos(gasType, null);
            }
            if(gasType!=-1 && deviceId!=-1){
                deviceRealTimeInfos = DeviceGasDataManager.getInstance().queryDeviceGasInfos(gasType, deviceId);
            }
            view.setHistory(deviceRealTimeInfos);
            view.hideProgress();
        } catch (Exception e) {
            view.hideProgress();
            view.showException(new BusinessErrorException(activity.getString(R.string.history_get_fail)));
        }
    }
}
